//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ForwardMeasureProcess.h"
using namespace Cephei::QL::Processes;
#include <gen/QL/Math/Array.h>
#include <gen/QL/StochasticProcess.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Processes::CForwardMeasureProcess::CForwardMeasureProcess (boost::shared_ptr<QuantLib::ForwardMeasureProcess>& childNative, Object^ owner) : CStochasticProcess(CForwardMeasureProcess::typeid)
{
#ifdef HANDLE
	_phForwardMeasureProcess = NULL;
#endif
	_ppForwardMeasureProcess = &childNative;
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppForwardMeasureProcess));
}
Cephei::QL::Processes::CForwardMeasureProcess::CForwardMeasureProcess (QuantLib::ForwardMeasureProcess& childNative, Object^ owner) : CStochasticProcess(CForwardMeasureProcess::typeid)
{
#ifdef HANDLE
	_phForwardMeasureProcess = NULL;
#endif
	_ppForwardMeasureProcess = new boost::shared_ptr<QuantLib::ForwardMeasureProcess> (&childNative);
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppForwardMeasureProcess));
    _ForwardMeasureProcessOwner = owner;
    _StochasticProcessOwner = owner;
}

Cephei::QL::Processes::CForwardMeasureProcess::CForwardMeasureProcess (CForwardMeasureProcess^ copy) : CStochasticProcess(CForwardMeasureProcess::typeid)
{
#ifdef HANDLE
	_phForwardMeasureProcess = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppForwardMeasureProcess = new boost::shared_ptr<QuantLib::ForwardMeasureProcess> (copy->GetShared());
        _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppForwardMeasureProcess));
    }
}
Cephei::QL::Processes::CForwardMeasureProcess::CForwardMeasureProcess (PLATFORM::Type^ t) : CStochasticProcess(CForwardMeasureProcess::typeid)
{
#ifdef HANDLE
	_phForwardMeasureProcess = NULL;
#endif
	if (!t->IsSubclassOf(CForwardMeasureProcess::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Processes::CForwardMeasureProcess::CForwardMeasureProcess (QuantLib::Handle<QuantLib::ForwardMeasureProcess>& childNative, Object^ owner)  : CStochasticProcess(CForwardMeasureProcess::typeid)
{
	_phForwardMeasureProcess = &childNative;
	_ppForwardMeasureProcess = &static_cast<boost::shared_ptr<QuantLib::ForwardMeasureProcess>>(childNative.currentLink());
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppForwardMeasureProcess));
    _ForwardMeasureProcessOwner = owner;
}
Cephei::QL::Processes::CForwardMeasureProcess::CForwardMeasureProcess (QuantLib::Handle<QuantLib::ForwardMeasureProcess> childNative)  : CStochasticProcess(CForwardMeasureProcess::typeid)
{
	_phForwardMeasureProcess = &childNative;
	_ppForwardMeasureProcess = &static_cast<boost::shared_ptr<QuantLib::ForwardMeasureProcess>>(childNative.currentLink());
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppForwardMeasureProcess));
}
#endif
#ifdef STRUCT
Cephei::QL::Processes::CForwardMeasureProcess::CForwardMeasureProcess (QuantLib::ForwardMeasureProcess childNative)  : CStochasticProcess(CForwardMeasureProcess::typeid)
{
#ifdef HANDLE
	_phForwardMeasureProcess = NULL;
#endif
	_ppForwardMeasureProcess = new boost::shared_ptr<QuantLib::ForwardMeasureProcess> (new QuantLib::ForwardMeasureProcess (childNative));
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppForwardMeasureProcess));
}
#endif

Cephei::QL::Processes::CForwardMeasureProcess::~CForwardMeasureProcess ()
{
    if (_ppForwardMeasureProcess != NULL)
    {
	    delete _ppForwardMeasureProcess;
        _ppForwardMeasureProcess = NULL;
    }
}
Cephei::QL::Processes::CForwardMeasureProcess::!CForwardMeasureProcess ()
{
    if (_ppForwardMeasureProcess != NULL)
    {
	    delete _ppForwardMeasureProcess;
    }
}
QuantLib::ForwardMeasureProcess& Cephei::QL::Processes::CForwardMeasureProcess::GetReference ()
{
    if (_ppForwardMeasureProcess == NULL) throw REFNEW NativeNullException ();
	return **_ppForwardMeasureProcess;
}
boost::shared_ptr<QuantLib::ForwardMeasureProcess>& Cephei::QL::Processes::CForwardMeasureProcess::GetShared ()
{
    if (_ppForwardMeasureProcess == NULL) throw REFNEW NativeNullException ();
	return *_ppForwardMeasureProcess;
}
QuantLib::ForwardMeasureProcess* Cephei::QL::Processes::CForwardMeasureProcess::GetPointer ()
{
    if (_ppForwardMeasureProcess == NULL) throw REFNEW NativeNullException ();
	return &**_ppForwardMeasureProcess;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ForwardMeasureProcess>& Cephei::QL::Processes::CForwardMeasureProcess::GetHandle ()
{
	if (_phForwardMeasureProcess == NULL)
	{
		_phForwardMeasureProcess = new Handle<QuantLib::ForwardMeasureProcess> (*_ppForwardMeasureProcess);
	}
	return *_phForwardMeasureProcess;
}
#endif
bool Cephei::QL::Processes::CForwardMeasureProcess::HasNative () 
{
	return (_ppForwardMeasureProcess != NULL);
}

Double Cephei::QL::Processes::CForwardMeasureProcess::GetForwardMeasureTime::get ()
{
    try
    {
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppForwardMeasureProcess)->getForwardMeasureTime ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Processes::IForwardMeasureProcess^ Cephei::QL::Processes::CForwardMeasureProcess::SetForwardMeasureTime (Double T)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Time _T = (QuantLib::Time)ValueHelper::Convert (T); //a
    	(*_ppForwardMeasureProcess)->setForwardMeasureTime ( _T );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

